package org.springcloud.plugin.framework.context;

import org.springcloud.plugin.common.constant.DiscoveryConstant;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;

/**
 * @Author yanggld
 * @Date 2019/12/17-10:52
 * @Description
 */
public class PluginContextAware implements ApplicationContextAware {

	private ApplicationContext applicationContext;
	private Environment environment;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
		this.environment = applicationContext.getEnvironment();
	}

	/**
	 * 获取规则的格式类型： xml 或者 json
	 * @return
	 */
	public String getConfigFormat() {
		return getConfigFormat(environment);
	}

	/**
	 * 获取规则的格式类型： xml 或者 json
	 * @param environment
	 * @return
	 */
	public static String getConfigFormat(Environment environment) {
		return environment.getProperty(DiscoveryConstant.SPRING_APPLICATION_CONFIG_FORMAT, String.class, DiscoveryConstant.XML_FORMAT);
	}

	public static String getApplicationName(Environment environment) {
		return environment.getProperty(DiscoveryConstant.SPRING_APPLICATION_NAME);
	}

	public Environment getEnvironment() {
		return environment;
	}
}
